<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">欢迎使用中萌智能系统</h3>
      </div>

      <el-form-item prop="UserName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="UserName"
          v-model="loginForm.UserName"
          placeholder="请输入账号"
          name="UserName"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            autocomplete="off"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

      <div style="position:relative">
        <div class="tips" @click="showRestPassword=true">
          <span>忘记密码</span>
        </div>
        <!-- <div class="tips">
          <span style="margin-right:18px;">Username : editor</span>
          <span>Password : any</span>
        </div>

        <el-button class="thirdparty-button" type="primary" @click="showDialog=true">
          Or connect with
        </el-button> -->
      </div>
    </el-form>

    <el-dialog title="找回密码" :visible.sync="showRestPassword">
      <restPassword v-if="showRestPassword" @closeDialog="showRestPassword = false" />
    </el-dialog>

    <!-- <el-dialog title="Or connect with" :visible.sync="showDialog">
      Can not be simulated on local, so please combine you own business simulation! ! !
      <br>
      <br>
      <br>
      <social-sign />
    </el-dialog> -->
    
    
    <!-- 动态粒子 -->
    <vue-particles
      color="#fff"
      :particlesOpacity="0.7"
      :particlesNumber="60"
      shapeType="circle"
      :particlesSize="2"
      linesColor="#fff"
      :linesWidth="1"
      :linesLinked="true"
      :linesOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="4"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
      class="lizi"
    >
    </vue-particles>

    <!-- 旋转环 -->
    <div class="circle">
      <div class="circle-left"></div>
      <div class="circle-right"></div>
    </div>

  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
import restPassword from './components/restPassword'
import crypto from 'crypto'

export default {
  name: 'Login',
  components: { restPassword },
  data() {
    return {
      loginForm: {
        UserName: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      showRestPassword: false,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    // if (this.loginForm.username === '') {
    //   this.$refs.username.focus()
    // } else if (this.loginForm.password === '') {
    //   this.$refs.password.focus()
    // }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock({ shiftKey, key } = {}) {
      if (key && key.length === 1) {
        if (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z')) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    // 登录
    handleLogin() {
      this.loading = true;
      let num = Math.floor(Math.random()*10);
      let str = '';
      for(var i=0;i<num;i++){
        str += Math.floor(Math.random()*10)
      }
      let passWord = '中萌智能城市' + this.loginForm.password;
      passWord = crypto.createHash('md5').update(passWord).digest('hex');
      passWord = crypto.createHash('md5').update(passWord + str).digest('hex');
      passWord = passWord + str + num;
      this.$axios({
        method: 'post',
        url: '/system/user/login',
        data: qs.stringify({ account: this.loginForm.UserName, password: passWord })
      }).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          // 获取登录者的userId 、userName、token存入本地
          this.$auth.setToken(res.data.token)
          this.$auth.setUserId(res.data.userId)
          this.$auth.setRoleId(res.data.roleId)
          this.$auth.setPermissionIds(res.data.permissionIdList)
          this.$auth.setUsername(res.data.username)

          this.loading = true
          this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
          this.loading = false
        } else {
          this.loading = false;
          this.$message(res.desc);
        }
      }).catch(() => {
        this.loading = false
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    resetPassword() {

    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #0a1d2d inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;




.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  background-image: url(../../assets/background_imge.png);
  background-size: (130% 130%);
  overflow: hidden;

  // 动态粒子
  #particles-js {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
  }

  //旋转环
  .circle {
    width: 540px;
    height: 540px;
    border-radius: 50%;
    position: absolute;
    top: 50px;
    left: calc(50% - 270px);
    mask: radial-gradient(transparent 235px, #000 235px);
    overflow: hidden;
    animation: circle 2s infinite linear;
  }

  .circle-left {
      width: 50%;
      height: 100%;
      background: linear-gradient(to bottom, #0f2f4a, #0d2337);
      float: left;
  }

  .circle-right {
      width: 50%;
      height: 100%;
      float: right;
      background: #0e1c26;
  }
  ///////////////////


  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    z-index: 1000;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .tips {
    cursor: pointer;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }


  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}

@keyframes circle {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
